<template>
  <div class="xtx-form">
    <Form
      ref="target"
      :validation-schema="mySchema"
      v-slot="{ errors }"
      autocomplete="off"
      class="xtx-form"
    >
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-user"></i>
          <Field
            :class="{ err: errors.account }"
            v-model="form.account"
            name="account"
            class="input"
            type="text"
            placeholder="请输入用户名"
          />
          <!-- <input class="input" type="text" placeholder="请输入用户名" /> -->
        </div>
        <div v-if="errors.account" class="error">{{ errors.account }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-phone"></i>
          <Field
            :class="{ err: errors.mobile }"
            v-model="form.mobile"
            name="mobile"
            class="input"
            type="text"
            placeholder="请输入手机号"
          />
          <!-- <input class="input" type="text" placeholder="请输入手机号" /> -->
        </div>
        <div v-if="errors.mobile" class="error">{{ errors.mobile }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-code"></i>
          <Field
            :class="{ err: errors.code }"
            v-model="form.code"
            name="code"
            class="input"
            type="text"
            placeholder="请输入验证码"
          />
          <!-- <input class="input" type="text" placeholder="请输入验证码" /> -->
          <span class="code" @click="sendCode">{{countTime === 0 ? '发送短信验证码' : countTime+'s后重新获取'}}</span>
        </div>
        <div v-if="errors.code" class="error">{{ errors.code }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-lock"></i>
          <Field
            :class="{ err: errors.password }"
            v-model="form.password"
            name="password"
            class="input"
            type="text"
            placeholder="请输入密码"
          />
          <!-- <input class="input" type="password" placeholder="请输入密码" /> -->
        </div>
        <div v-if="errors.password" class="error">{{ errors.password }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-lock"></i>
          <Field
            :class="{ err: errors.reqPassword }"
            v-model="form.reqPassword"
            name="reqPassword"
            class="input"
            type="text"
            placeholder="请确认密码"
          />
          <!-- <input class="input" type="password" placeholder="请确认密码" /> -->
        </div>
        <div v-if="errors.reqPassword" class="error">{{ errors.reqPassword }}</div>
      </div>
    </Form>
    <a href="javascript:;" class="submit"  @click="qqBind">立即提交</a>
  </div>
</template>

<script>
import { Form, Field } from 'vee-validate'
import schema from '@/utils/vee-validate-schema'
import { reactive, ref } from 'vue'
import { userQQPatchCode, userQQPatchLogin } from '@/api/login'
import { useCountDown } from '@/useFn'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackPatch',
  props: {
    unionId: {
      type: String
    }
  },
  components: {
    Form,
    Field
  },
  setup (props) {
    const mySchema = { // 校验规则
      account: schema.account,
      mobile: schema.mobile,
      code: schema.code,
      password: schema.password,
      reqPassword: schema.reqPassword
    }
    // 表单双向绑定的数据
    const form = reactive({
      account: '',
      mobile: '',
      code: '',
      password: '',
      reqPassword: ''
    })
    // 通过ref 拿到Form实例对象
    const target = ref(null)

    // 倒计时
    const { countTime, start } = useCountDown()
    // 点击发送验证码
    function sendCode () {
      // 先判断用户名和手机号是否合法  合法的话调用接口 开启倒计时 不合法的话错误提示
      if (countTime.value > 0) return // 如果正处于倒计时时间  不能再次点击 请求
      const account = mySchema.account(form.account)
      const mobile = mySchema.mobile(form.mobile)
      // 当上面两个值全部为true时  表示校验通过
      if (account === true && mobile === true) {
        // 开始调用发送验证码的接口
        userQQPatchCode(form.mobile).then(res => {
          console.log('验证码发送成功', res)
          // 开始倒计时
          start(60)
        })
      } else { // 校验没过  错误提示
        // 对表单某项进行单独校验  formCom.value.setFieldError('mobile', errorMsg) （字段,错误信息）
        target.value.setFieldError('account', account)
        target.value.setFieldError('mobile', mobile)
      }
    }

    const store = useStore()
    const router = useRouter()
    // 点击注册
    async function qqBind () {
      // 判断两个密码框的值是否一样  一样的话给 schema.reqPassword第二个参数传true
      console.log(target)
      // 先兜底校验
      const valid = await target.value.validate()
      console.log(valid)
      if (valid) {
        const res = await userQQPatchLogin({
          unionId: props.unionId,
          mobile: form.mobile,
          code: form.code,
          account: form.account,
          password: form.password
        })
        console.log(res)
        // console.log('绑定成功', res)  // res 返回的是当前用户的所有信息
        // 存用户信息
        store.commit('user/setUser', res.data.result)
        // 跳转首页
        router.push('/')
      }
    }

    return {
      mySchema,
      form,
      sendCode,
      countTime,
      qqBind,
      target
    }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 100px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
